#include"wait.h"
#include"global.h"
#include"thread.h"
#include"memory.h"

pid_t sys_wait(int32_t* status){
	task_tcb* parent;
	task_tcb* child;
	pid_t pid;

	parent=current_thread();
	while(true){
		child=find_hanging_child(parent);
		if(child){
			*status=child->exit_status;
			pid=child->pid;
			//free task tcb resource
			thread_exit(child,false);
			return pid;
		}
		child=find_child(parent);
		if(!child){
			return -1;
		}
		thread_block(TASK_WAITING);
	}
	return -1;
}
